package com.platform.modules.wallet.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.wallet.dao.ChatTestMsgDao;
import com.platform.modules.wallet.domain.ChatTestMsg;
import com.platform.modules.wallet.enums.PullStatusEnum;
import com.platform.modules.wallet.service.ChatTestMsgService;
import com.platform.modules.wallet.vo.TestMsgVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service("chatTestMsgService")
public class ChatTestMsgServiceImpl extends BaseServiceImpl<ChatTestMsg> implements ChatTestMsgService {

    @Resource
    private ChatTestMsgDao chatTestMsgDao;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(chatTestMsgDao);
    }

    @Override
    public ChatTestMsg getOneMsg(Long userId){

        return new ChatTestMsg().selectOne(
                new LambdaQueryWrapper<ChatTestMsg>()
                        .eq(ChatTestMsg::getUserId, userId)
                        .eq(ChatTestMsg::getPullStatus, PullStatusEnum.INIT)
        );
    }

    @Override
    public ChatTestMsg pull(Long userId){

        ChatTestMsg oneMsg = getOneMsg(userId);

        if (ObjectUtil.isEmpty(oneMsg)){
            return null;
        }

        oneMsg.setPullStatus(PullStatusEnum.SUCCESS);
        oneMsg.setPullTime(DateUtil.date());
        updateById(oneMsg);

        return oneMsg;
    }

    public void add(TestMsgVo vo){
        ChatTestMsg bean = getBean();
        bean.setUserId(vo.getUserId());
        bean.setContent(vo.getContent());
        this.add(bean);
    }

    private ChatTestMsg getBean(){
        ChatTestMsg bean = new ChatTestMsg();
        bean.setPullStatus(PullStatusEnum.INIT);
        bean.setCreateTime(DateUtil.date());
        return bean;
    }
}
